package com.dankegongyu.lib.http.api;

import com.dankegongyu.lib.http.DKHttp;
import com.dankegongyu.lib.http.interceptor.DKBaseInterceptor;
import com.dankegongyu.lib.http.request.DeleteRequest;
import com.dankegongyu.lib.http.request.GetRequest;
import com.dankegongyu.lib.http.request.PostRequest;
import com.dankegongyu.lib.http.request.PutRequest;

import java.util.List;

/**
 * @author wupuquan
 * @date 2020/6/18 14:05
 */
public abstract class DKBaseApi implements IBaseApi {

    private DKHttp.Builder mBuilder;

    public DKBaseApi() {
        mBuilder = DKHttp.Builder.newInstance();
        // 拦截器
        List<DKBaseInterceptor> interceptors = interceptors();
        if (interceptors != null && !interceptors.isEmpty()) {
            for (DKBaseInterceptor interceptor : interceptors) {
                mBuilder.addInterceptor(interceptor);
            }
        }
        // 超时
        long timeout = timeout();
        if (timeout > 0) {
            mBuilder.setTimeout(timeout);
        } else {
            mBuilder.setConnectTimeout(connectTimeout());
            mBuilder.setReadTimeout(readTimeout());
            mBuilder.setWriteTimeout(writeTimeout());
        }
    }

    @Override
    public long timeout() {
        return 0;
    }

    protected long connectTimeout() {
        return 0;
    }

    protected long readTimeout() {
        return 0;
    }

    protected long writeTimeout() {
        return 0;
    }

    private String getFullUrl(String path) {
        return baseUrl() + path;
    }

    public GetRequest get(String path) {
        return DKHttp.get(getFullUrl(path), mBuilder);
    }

    public PostRequest post(String path) {
        return DKHttp.post(getFullUrl(path), mBuilder);
    }

    public PutRequest put(String path) {
        return DKHttp.put(getFullUrl(path), mBuilder);
    }

    public DeleteRequest delete(String path) {
        return DKHttp.delete(getFullUrl(path), mBuilder);
    }

//    public static <T> T getService(Class<T> service) {
//        return getInstance().getRetrofit().create(service);
//    }
}

// kotlin
//object TestApi : DKHttpBaseApi() {
//    override fun baseUrl(): String {
//
//    }
//
//    override fun interceptors(): MutableList<Interceptor>? {
//        return
//    }
//}

// java
//class TestApi extends DKHttpBaseApi {
//
//    @Override
//    public String baseUrl() {
//        return null;
//    }
//
//    @Override
//    public List<Interceptor> interceptors() {
//        return null;
//    }
//
//    private static class SingletonHolder {
//        private static final TestApi INSTANCE = new TestApi();
//    }
//
//    private static TestApi getInstance() {
//        return SingletonHolder.INSTANCE;
//    }
//}
